import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {ILoginResult, LOCAL_LOGIN} from "../../common/common";


@Component({
  selector: 'app-page-login',
  templateUrl: './page-login.component.html',
  styleUrls: ['./page-login.component.scss']
})
export class PageLoginComponent implements OnInit {

  @Output() public loginResult: EventEmitter<ILoginResult> = new EventEmitter<ILoginResult>()

  showLoading = false

  constructor() {
  }

  ngOnInit(): void {
  }

  login(): ILoginResult {
    let userJson: string;

    let userLogin = false;
    let userInfo;
    let token;

    //TODO 数据来源

    userJson = "{\n" +
      "    \"userId\":1,\n" +
      "    \"userHead\":\"\",\n" +
      "    \"userName\":\"Squbirreland\"\n" +
      "}";
    token = "token[123456]---?"

    if (userJson == null || userJson == "") {
      userJson = "{\n" +
        "    \"userId\":0,\n" +
        "    \"userHead\":\"\",\n" +
        "    \"userName\":\"游客\"\n" +
        "}"
    } else {
      userLogin = true
    }

    userInfo = JSON.parse(userJson);
    return {success: userLogin, userInfo: userInfo, token: token}
  }

  loginUp() {
    this.showLoading = true;
    let iLoginResult = this.login();
    this.showLoading = false;
    localStorage.setItem(LOCAL_LOGIN, JSON.stringify(iLoginResult))
    this.loginResult.emit(iLoginResult)
  }

}
